﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class createProduct : System.Web.UI.Page
    {
        DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Create product";
            if (!IsPostBack)
            {
                loadDropdownData(1);
                loadLanguage();
                loadPartner();
                loadDataforProductRelative(1);
            }
        }

        private void loadDataforProductRelative(int idlanguage) {
            var dataproduct = from pr in dc.Products
                              join prl in dc.ProductsLanguages
                              on pr.id equals prl.productsId
                              where pr.status==true
                              select new
                              {

                                  ProductID = pr.id,
                                  ProductName = prl.name,
                                  idLang=prl.languageId

                              } ;

            ListView1.DataSource = dataproduct.Where(z => z.idLang == idlanguage);
            ListView1.DataBind();
        }
        private void loadPartner() {
            DropDownList3.DataSource = dc.Partners.Where(z => z.status == true); ;
            DropDownList3.DataTextField = "name";
            DropDownList3.DataValueField = "id";
            DropDownList3.DataBind();
        }
        private void loadDropdownData(int language) {

            DropDownList1.DataSource = dc.CategoriesLanguages.Where(z => z.languageId == language && z.Category.status == true);
            DropDownList1.DataTextField = "name";
            DropDownList1.DataValueField = "categoriesId";
            DropDownList1.DataBind();
        
        }

        private void loadLanguage(){
          DropDownList2.DataSource=  dc.Languages;
          DropDownList2.DataTextField = "name";
          DropDownList2.DataValueField = "id";
          DropDownList2.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {




            string pro_en = TextboxName_en.Value.Trim();
            string pro_fr = TextboxName_fr.Value.Trim();
            string pro_gr = TextboxName_gr.Value.Trim();
            
            if (string.IsNullOrEmpty(pro_en) && string.IsNullOrEmpty(pro_fr) && string.IsNullOrEmpty(pro_gr))
            {
                Label_proname.Text = "Productname name is required";
            }
            else {
                if (Page.IsValid)
                {

                    int q = string.IsNullOrEmpty(textboxQuantity.Value) ? 0 : Convert.ToInt32(textboxQuantity.Value);
                    double d = string.IsNullOrEmpty(textboxDiscount.Value) ? 0 : Convert.ToDouble(textboxDiscount.Value);
                    Product p = new Product
                    {
                        price=Convert.ToDecimal(textboxPrice.Value),
                      
                       
                        quantity=Convert.ToInt32(q),
                        status=true,
                        categoriesId=Convert.ToInt32(DropDownList1.SelectedValue),
                        features=Convert.ToBoolean(Convert.ToInt32(DropDownList1.SelectedValue)),
                        discount=d,
                        PartnersId=Convert.ToInt32(DropDownList3.SelectedValue),



                        
                    };
                    dc.Products.InsertOnSubmit(p);
                    dc.SubmitChanges();

                    if (!string.IsNullOrEmpty(pro_en))
                    {
                        insertProLanguage(p, pro_en, TextboxDescription_en.Value, 1);
                    }
                    else {
                        insertProLanguage(p, " ", TextboxDescription_en.Value, 1);
                    }

                    if (!string.IsNullOrEmpty(pro_fr))
                    {
                        insertProLanguage(p, pro_fr, TextboxDescriptionFr.Value, 2);
                    }
                    else
                    {
                        insertProLanguage(p, " ", TextboxDescriptionFr.Value, 2);
                    }
                    if (!string.IsNullOrEmpty(pro_gr))
                    {
                        insertProLanguage(p, pro_gr, TextboxDescriptionGr.Value, 3);
                    }
                    else
                    {
                        insertProLanguage(p, " ", TextboxDescriptionGr.Value, 3);
                    }
                    string photo1 = textboxURL1.Value.Trim();
                    string photo2 = textboxURL2.Value.Trim();
                    string photo3 = textboxURL3.Value.Trim();

                    if (!string.IsNullOrEmpty(photo1))
                    {
                        insertPhoto(p, photo1);
                    }
                    if (!string.IsNullOrEmpty(photo2))
                    {
                        insertPhoto(p, photo2);
                    } 
                    if (!string.IsNullOrEmpty(photo3))
                    {
                        insertPhoto(p, photo3);
                    }

                    //insert into product relative
                    CheckBox tb = null;
                    ListViewItem Item = null;
                    List<int> listHiddenValue = new List<int>();
                    foreach (ListViewDataItem item in ListView1.Items)
                    {

                        Item = item;
                        tb = ((CheckBox)(Item.FindControl("CheckBox1")));
                        if (tb.Checked)
                        {
                            HiddenField hd = tb.FindControl("HiddenField1") as HiddenField;
                            listHiddenValue.Add(Convert.ToInt32(hd.Value));
                        }
                    }

                    foreach (int item in listHiddenValue)
                    {
                        ProductsRelative pr = new ProductsRelative { 
                        
                            productsId_1=p.id,
                            productsId_2=item,
                        };

                        dc.ProductsRelatives.InsertOnSubmit(pr);
                        dc.SubmitChanges();
                    }

                    Response.Redirect("productList.aspx");
                }
            }
        }

        private void insertProLanguage(Product cat, string name, string description, int languageId)
        {

            ProductsLanguage pl = new ProductsLanguage
            {
               
                details=description,
                name = name,
                languageId = languageId,
                productsId=cat.id
            };

            dc.ProductsLanguages.InsertOnSubmit(pl);
            dc.SubmitChanges();


        }

        private void insertPhoto(Product pro, string name)
        {

            ProductsPhoto pp = new ProductsPhoto
            {
                name=name,
                productsId=pro.id,
               
            };

            dc.ProductsPhotos.InsertOnSubmit(pp);
            dc.SubmitChanges();


        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idLanguage =Convert.ToInt32( DropDownList2.SelectedValue);
            loadDropdownData(idLanguage);
            loadDataforProductRelative(idLanguage);
        }
    }
}